<?php
namespace app\common\component\user_wallet_upstart_freeze;

use app\common\model\UserWalletUpstart;
use app\common\model\UserWalletUpstartFreeze;

use app\common\component\user_wallet_upstart_log\UserWalletUpstartLogService;

class UserWalletUpstartFreezeService {
	public function thaw() {
		$selectUserWalletUpstartFreeze = UserWalletUpstartFreeze::where('is_freeze = 0 AND end_time < ' . time()) -> select();
		if(empty($selectUserWalletUpstartFreeze)) {
			return false;
		}
		foreach($selectUserWalletUpstartFreeze as $userWalletUpstartFreeze) {
			$userWalletUpstartFreeze -> is_freeze = 1;

			\think\Db::startTrans();
			try {
				if(!$userWalletUpstartFreeze -> save()) {
					throw new \Exception('user_id:' . $userWalletUpstartFreeze -> user_id . '---' . '解冻失败');
				}
	
				if($userWalletUpstartFreeze -> price > 0) {
					$resultUserWalletUpstartLog1 = (new UserWalletUpstartLogService) -> log(4, 1, $userWalletUpstartFreeze -> price, $userWalletUpstartFreeze -> user_id, '土豪冻结本金转入钱包', 1);
					if($resultUserWalletUpstartLog1['code'] !== 0) {
						throw new \Exception('user_id:' . $userWalletUpstartFreeze -> user_id . '---' . $resultUserWalletUpstartLog1['message']);
					}
				}
	
				if($userWalletUpstartFreeze -> interest > 0) {
					$resultUserWalletUpstartLog2 = (new UserWalletUpstartLogService) -> log(5, 1, $userWalletUpstartFreeze -> interest, $userWalletUpstartFreeze -> user_id, '冻结利息转入钱包', 1);
					if($resultUserWalletUpstartLog2['code'] !== 0) {
						throw new \Exception('user_id:' . $userWalletUpstartFreeze -> user_id . '---' . $resultUserWalletUpstartLog2['message']);
					}
				}
	
				if(!UserWalletUpstart::where('user_id', $userWalletUpstartFreeze -> user_id) -> setInc('price', $userWalletUpstartFreeze -> price + $userWalletUpstartFreeze -> interest)) {
					throw new \Exception('user_id:' . $userWalletUpstartFreeze -> user_id . '---' . '解冻失败');
				}
				\think\Db::commit();
			} catch(\Exception $e) {
				\think\Db::rollback();

				\think\Log::error($e -> getMessage());
			}
		}
	}

	public function draw($user_wallet_upstart_freeze_id, $key) {
		$findUserWalletUpstartFreeze = UserWalletUpstartFreeze::where('user_wallet_upstart_freeze_id = ' . $user_wallet_upstart_freeze_id) -> find();
		if(!$findUserWalletUpstartFreeze) {
			return [
    			'code' => -1,
    			'message' => '冻结订单不存在'
    		];
		}

		$freezeOperate = json_decode($findUserWalletUpstartFreeze -> freeze_operate, true);
		if(!isset($freezeOperate[$key])) {
			return [
    			'code' => -1,
    			'message' => '冻结订单不存在'
    		];
		}

		$currentTime = time();
		$freezeOperate[$key]['is_draw'] = 1;
		$freezeOperate[$key]['end_time'] = $currentTime;

		for($i=$key+1;$i<=5;$i++) {
			$freezeOperate[$i]['create_time'] = $currentTime + ($i-2) * 86400;
			$freezeOperate[$i]['end_time'] = $currentTime + ($i-1) * 86400;
		}

		$findUserWalletUpstartFreeze -> remain_price -= $freezeOperate[$key]['price'];
		if($findUserWalletUpstartFreeze -> remain_price <= 0 || $key == 5) {
			$findUserWalletUpstartFreeze -> is_freeze = 1;
		}

		$findUserWalletUpstartFreeze -> freeze_operate = json_encode($freezeOperate);

		\think\Db::startTrans();
		try {
			if(!$findUserWalletUpstartFreeze -> save()) {
				throw new \Exception('领取失败');
			}

			$resultUserWalletUpstartLog = (new UserWalletUpstartLogService) -> log(4, 1, $freezeOperate[$key]['price'], $findUserWalletUpstartFreeze -> user_id, '土豪冻结金钱转入钱包', 1);
			if($resultUserWalletUpstartLog['code'] !== 0) {
				throw new \Exception('领取失败');
			}

			if(!UserWalletUpstart::where('user_id', $findUserWalletUpstartFreeze -> user_id) -> setInc('price', $freezeOperate[$key]['price'])) {
				throw new \Exception('领取失败');
			}

			\think\Db::commit();
		} catch(\Exception $e) {
			\think\Db::rollback();
			return [
    			'code' => -1,
    			'message' => $e-> getMessage()
    		];
		}

		return [
			'code' => 0,
			'message' => '领取成功'
		];
	}

	public function create($data) {
		$UserWalletUpstartFreeze = new UserWalletUpstartFreeze($data);

		if($UserWalletUpstartFreeze -> save()) {
    		return [
    			'code' => 0,
    			'message' => '创建土豪区冻结成功',
    			'data' => [
    				'user_wallet_upstart_freeze' => $UserWalletUpstartFreeze
    			]
    		];
    	}

    	return [
			'code' => -1,
			'message' => '创建土豪区冻结失败'
		];
	}
}
?>